package com.lmx.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.AdminUserService;
import com.lmx.persistence.service.LoginService;
import com.lmx.persistence.service.RoleService;
import com.lmx.repoDomain.tmodel.Role;//huibati
import com.lmx.repoDomain.tmodel.UserPageInfo;
import com.lmx.repoDomain.tmodel.UserPrivate;
import com.lmx.repoDomain.tmodel.business.User;//huibati;


@Controller
@RequestMapping("/backStand")
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;
    
    @Autowired
    RoleService roleService;
    
    @Autowired
    LoginService loginService;
    
    @Autowired
    HttpServletRequest request;
    
    //获取后台管理员用户列表
    @RequestMapping(value = "/getAdminUserList", method = RequestMethod.GET)
    public String  getAdminUserList(ModelMap model,UserPageInfo userPageInfo) {
    	
    	Map<String ,Object> values = new HashMap<String ,Object>();
    	
    	//验证当前页数
    	if(!String.valueOf(userPageInfo.getPage()).isEmpty()){
    		int pageNum = (userPageInfo.getPage()-1)*userPageInfo.getSize();
    		values.put("pageNum", pageNum);
    		values.put("size", userPageInfo.getSize());
    		model.put("page", userPageInfo.getPage());
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	String loginName = userPageInfo.getLoginName();
    	values.put("loginName", loginName);//用户名
    	model.put("loginName", loginName==null?loginName="":loginName);
    	
    	String name = userPageInfo.getName();
    	values.put("name", name);//名称
    	model.put("name", name==null?name="":name);
    	
    	String phone = userPageInfo.getPhone();
    	values.put("phone", phone);//电话号码
    	model.put("phone", phone==null?phone="":phone);
    	
    	String status = userPageInfo.getStatus();
    	values.put("status", status);//状态
    	model.put("status", status==null?status="":status);
    	
    	String roleCode = userPageInfo.getRoleCode();
    	values.put("roleCode", roleCode);//角色code
    	model.put("roleCode", roleCode==null?roleCode="":roleCode);
    	
    	//获取后台管理员用户列表
    	List<Map<String ,Object>> userList = adminUserService.getAdminUserList(values);
    	//获取后台管理员用户列表总条数
    	int count = adminUserService.getAdminUserListCount(values);
    	
    	model.put("userList", userList);
    	model.put("count", count);
    	
        return "page/role/adminUserList";
    }    
    
    //获取后台角色列表
    @RequestMapping(value = "/getBackRoleList", method = RequestMethod.GET)
    @ResponseBody
    public String  getBackRoleList() {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	//获取后台角色列表
    	List<Map<String ,Object>> roleList = roleService.getBackRoleList();
    	
    	msgMap.put("flag", 1);
    	msgMap.put("roleList", roleList);
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //添加管理员用户
    @RequestMapping(value = "/addAdminUser", method = RequestMethod.GET)
    @ResponseBody
    public String  addAdminUser(String loginName ,String password ,String name ,String roleCode ,String phone ,String email ,String sex , String remark) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	User user = new User();
    	user.setLoginName(loginName);
    	user.setPassword(password);
    	user.setName(name);
    	Role role = new Role();
    	role.setCode(roleCode);
    	user.setRole(role);
    	user.setStaffTel(phone);
    	user.setStaffMail(email);
    	//user.setSex(Short.valueOf(sex));
    	user.setRemark(remark);
    	
    	UserPrivate userPrivate = new UserPrivate();
    	UserPrivate resuleUser = loginService.getUserInfo(userPrivate);
    	String loginNameStr = resuleUser.getLoginName();
    	if(loginNameStr != null && !loginNameStr.isEmpty()){
    		msgMap.put("flag", "0");//用户名已存在
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	
    	adminUserService.addAdminUser(user);
    	
    	msgMap.put("flag", "1");//添加成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //删除管理员用户
    @RequestMapping(value = "/deleteAdminUser", method = RequestMethod.GET)
    @ResponseBody
    public String  deleteAdminUser(String userID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	if(userID != null && !userID.isEmpty()){
    		adminUserService.deleteAdminUser(Long.parseLong(userID));
    	}else{
    		msgMap.put("flag", "0");
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
   
    //修改管理员用户
    @RequestMapping(value = "/updateAdminUser", method = RequestMethod.GET)
    @ResponseBody
    public String  updateAdminUser(String userID  ,String password ,String name ,String roleCode ,String phone ,String email ,String sex , String remark) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	User user = new User();
    	user.setId(Long.parseLong(userID));
    	user.setPassword(password);
    	user.setName(name);
    	Role role = new Role();
    	role.setCode(roleCode);
    	user.setRole(role);
    	user.setStaffTel(phone);
    	user.setStaffMail(email);
    	//user.setSex(Short.valueOf(sex));
    	user.setRemark(remark);
    	
    	adminUserService.updateAdminUser(user);
    	
    	msgMap.put("flag", "1");//修改成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //获取用户详情
    @RequestMapping(value = "/getAdminUser", method = RequestMethod.GET)
    @ResponseBody
    public String  getAdminUser(String userID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	User user = new User();
    	user.setId(Long.parseLong(userID));
    	
    	Map<String ,Object> userMap = adminUserService.getAdminUser(user);
    	
    	msgMap.put("flag", "1");//修改成功
    	msgMap.put("userMap", userMap);
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
}
